<?php
namespace Admin\Controller;
use Think\Controller;
class MemberController extends AdminController {
    public function index(){
        $User = M('User');//实例化AdminUser对象
        $count = $User->count();// 查询满足要求的总记录数
        $itemNum = 6; //每页显示的记录数
        $show = c_page($count,"个会员");

        //page方法分页
        $data = $User
            ->alias('a')
            ->field('a.*')
            ->order('a.u_time desc, a.id desc')
            ->page(I('get.p',1) . ',' . $itemNum)
            ->select();

        if(IS_AJAX){
            $json['data'] = $data;
            $json['page'] = $show;
            $this->ajaxReturn($json);
        }else{
            $this->assign('title','会员列表');//标题
            $this->assign('data',$data);// 赋值数据集
            $this->assign('page',$show);// 赋值分页输出
            $this->display();
        }
    }

    public function detail($id = 0){
        if($id < 1){
            $this->error("非法操作");
        }
        $one  = M('User')
            ->alias('a')
            ->where(array('a.id'=>I('get.id')))
            ->field('a.*,p.city as p_city,p.prov as p_prov,p.dist as p_dist,p.address as p_address,p.username as p_name,p.mobile as p_mobile')
            ->join('__ADDRESS__ as p on a.address_id = p.id','left')
            ->select();
        if($one){
            $wh['id'] = array('neq',$one[0]['address_id']);
            $wh['user_id'] = $one[0]['id'];
            $two = M('Address')->where($wh)->select();
//            var_dump($two);
            $one[0]['addr_list']= $two;
//                        var_dump($one);
//            exit;
            $this->ajaxReturn($one[0]);
        }else{
            $this->ajaxReturn(0);
        }
    }



    public function delete($id = 0){
        if($id<1){
            $this->error('参数错误,至少选择一条数据');
        }
        if(!is_array($id)){
            $res = M('User')->delete($id);
        }else{
            $wh['id'] = array('in',implode(',', $id));
            $res = M('User')->where($wh)->delete();
        }
        if($res !== false){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败：',U('index'));
        }
    }


}
?>